package com.example.moshudriver.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.BeeFramework.model.BusinessResponse;
import com.BeeFramework.view.CopartnerDialog;
import com.sgr.dykdriver.R;
import com.example.moshudriver.adapter.VipListAdapter;
import com.example.moshudriver.model.DrawModel;
import com.example.moshudriver.model.UserBalanceModel;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.MESSAGE;
import com.example.moshudriver.protocol.USER;
import com.example.moshudriver.protocol.userprofileResponse;
import com.external.androidquery.callback.AjaxStatus;
import com.external.maxwin.view.IXListViewListener;
import com.external.maxwin.view.XListView;

public class MyVipActivity extends Activity implements OnClickListener,
		BusinessResponse, IXListViewListener {

	private XListView couponXList;
	private ImageView mEmptyView;
	private VipListAdapter couponAdapter;
	TextView top_view_title;
	EditText recharge_edit;
	ImageView top_view_back;
	UserBalanceModel withdrawModel;
	DrawModel withdraw_list;
	private int mDriverId = -1;
	private CopartnerDialog copartnerDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_coupons);

		initView();
		withdraw_list = new DrawModel(this);
		withdraw_list.addResponseListener(this);
		withdraw_list.getVipC();

	}

	private void initView() {
		top_view_title = (TextView) findViewById(R.id.top_view_title);
		top_view_title.setText(R.string.my_vip);
		recharge_edit = (EditText) findViewById(R.id.recharge_edit);
		top_view_back = (ImageView) findViewById(R.id.top_view_back);
		top_view_back.setOnClickListener(this);
		mEmptyView = (ImageView) findViewById(R.id.e0_empty_view);
		mEmptyView.setVisibility(View.GONE);

		couponXList = (XListView) findViewById(R.id.coupon_xlist);
		couponXList
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						USER user = withdraw_list.userList.get(position - 1);
						mDriverId = user.id;
						try {
							JSONObject obj = new JSONObject(user.user);
							String name = obj.getString("name");
							copartnerDialog = new CopartnerDialog(
									MyVipActivity.this, name, user.money,
									user.ordercuont);
							copartnerDialog.show();
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

						copartnerDialog.positive
								.setOnClickListener(new View.OnClickListener() {

									@Override
									public void onClick(View v) {
										// TODO Auto-generated method stub
										copartnerDialog.dismiss();
									}
								});
						// withdraw_list.userList

					}
				});

		couponXList.setPullRefreshEnable(true);
		couponXList.setPullLoadEnable(true);
		couponXList.setXListViewListener(this, 2);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.top_view_back:
			finish();
			break;
		}

	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		couponXList.stopRefresh();
		couponXList.stopLoadMore();
		// if (url.endsWith(ApiInterface.USER_VIP)) {
		//
		// if (null != jo) {
		//
		// userprofileResponse response = new userprofileResponse();
		// response.fromJson(jo);
		//
		// couponXList.loadMoreShow();
		// mEmptyView.setVisibility(View.GONE);
		// couponXList.setVisibility(View.VISIBLE);
		// if (null == couponAdapter) {
		// couponAdapter = new VipListAdapter(MyVipActivity.this,
		// withdraw_list.userList);
		// couponXList.setAdapter(couponAdapter);
		// } else {
		// couponAdapter.notifyDataSetChanged();
		// }
		//
		// } else {
		// if (withdraw_list.couponList.size() == 0) {
		// mEmptyView.setVisibility(View.VISIBLE);
		// couponXList.setVisibility(View.GONE);
		// }
		// }
		// }
		if (url.endsWith(ApiInterface.USER_VIPC)) {

			if (null != jo) {

				userprofileResponse response = new userprofileResponse();
				response.fromJson(jo);

				couponXList.loadMoreShow();
				mEmptyView.setVisibility(View.GONE);
				couponXList.setVisibility(View.VISIBLE);
				if (null == couponAdapter) {
					couponAdapter = new VipListAdapter(MyVipActivity.this,
							withdraw_list.userList);
					couponXList.setAdapter(couponAdapter);
				} else {
					couponAdapter.notifyDataSetChanged();
				}

			} else {
				if (withdraw_list.couponList.size() == 0) {
					mEmptyView.setVisibility(View.VISIBLE);
					couponXList.setVisibility(View.GONE);
				}
			}
		}

	}

	@Override
	public void onRefresh(int id) {
		withdraw_list.getVipC();
		withdraw_list.getVip();
	}

	@Override
	public void onLoadMore(int id) {
		// Toast.makeText(this, "onLoadMore", 2000).show();

	}

}